package com.museum.mession.dig;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.museum.counst.USERS;
import com.museum.entities.action.ActionResponse;
import com.museum.entities.userinfo.UserData;
import com.museum.entities.useritem.UserItem;
import com.museum.utils.Sentor;
import com.museum.utils.Utils;

public class AccountDigger implements Runnable {
	private static final Log log = LogFactory.getLog(AccountDigger.class);
	private String username;
	private String userid;
	private String cookie;
	private int x;
	private int y;
	private static final Sentor sentor = new Sentor();

	public AccountDigger(int userIndex) {
		this(userIndex, 1, 1);
	}

	public AccountDigger(int userIndex, int x, int y) {
		username = USERS.users[userIndex];
		userid = USERS.uids[userIndex];
		cookie = USERS.cookies[userIndex];
	}

	@Override
	public void run() {
		UserData ud = sentor.userInfo().getData();
		int digBatch = ud.getDigbatch();
		if (digBatch != 0) {
			sentor.digDone();
		}
		while (true) {

			int sinew = ud.getSinew_now();
			if (sinew == 0) {
				UserItem ui = sentor.mycwList();
				if (ui.getData().getTotal() == ui.getData().getMax()) {
					sentor.sell("all");
				}
				sentor.buy("402", 1);
				rest();
				sentor.use("402");
				rest();
			}
			ActionResponse ar = sentor.dig(x, y);
			String err = ar.getErr();
			if (!Utils.isBlank(err)) {
				log.error(err);
			} else if ("由于您的储物间已满，所以此宝物自动丢弃！<BR>".equals(ar.getData().getErr())) {
				sentor.sell("all");
			}
			String text = ar.getData().getText();
			log.info(text);
			ud = ar.getData().getUserinfo();
			rest();
		}
	}

	public void rest() {
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
